package o;

import android.app.Activity;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.res.Resources;
import android.graphics.Rect;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.MeasureSpec;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.widget.TextView;
import androidx.annotation.RestrictTo;
import androidx.annotation.RestrictTo.d;
import o.ah.e;
import o.ah.f;

@RestrictTo({d.LIBRARY_GROUP})
class df {
    private final int[] ʻ = new int[2];
    private final int[] ʽ = new int[2];
    private final View ˊ;
    private final TextView ˋ;
    private final Context ˎ;
    private final LayoutParams ˏ = new LayoutParams();
    private final Rect ॱ = new Rect();

    df(Context context) {
        this.ˎ = context;
        this.ˊ = LayoutInflater.from(this.ˎ).inflate(ah$j.ˈ, null);
        this.ˋ = (TextView) this.ˊ.findViewById(ah$i.ʿ);
        this.ˏ.setTitle(getClass().getSimpleName());
        this.ˏ.packageName = this.ˎ.getPackageName();
        this.ˏ.type = 1002;
        this.ˏ.width = -2;
        this.ˏ.height = -2;
        this.ˏ.format = -3;
        this.ˏ.windowAnimations = f.ॱ;
        this.ˏ.flags = 24;
    }

    void ˏ(View view, int i, int i2, boolean z, CharSequence charSequence) {
        if (ˊ()) {
            ˏ();
        }
        this.ˋ.setText(charSequence);
        ˋ(view, i, i2, z, this.ˏ);
        ((WindowManager) this.ˎ.getSystemService("window")).addView(this.ˊ, this.ˏ);
    }

    void ˏ() {
        if (ˊ()) {
            ((WindowManager) this.ˎ.getSystemService("window")).removeView(this.ˊ);
        }
    }

    boolean ˊ() {
        return this.ˊ.getParent() != null;
    }

    private void ˋ(View view, int i, int i2, boolean z, LayoutParams layoutParams) {
        int i3;
        layoutParams.token = view.getApplicationWindowToken();
        int dimensionPixelOffset = this.ˎ.getResources().getDimensionPixelOffset(e.ʼ);
        if (view.getWidth() < dimensionPixelOffset) {
            i = view.getWidth() / 2;
        }
        if (view.getHeight() >= dimensionPixelOffset) {
            dimensionPixelOffset = this.ˎ.getResources().getDimensionPixelOffset(e.ॱॱ);
            i3 = i2 + dimensionPixelOffset;
            dimensionPixelOffset = i2 - dimensionPixelOffset;
        } else {
            i3 = view.getHeight();
            dimensionPixelOffset = 0;
        }
        layoutParams.gravity = 49;
        int dimensionPixelOffset2 = this.ˎ.getResources().getDimensionPixelOffset(z ? e.ˏॱ : e.ˋॱ);
        View ˎ = ˎ(view);
        if (ˎ == null) {
            Log.e("TooltipPopup", "Cannot find app view");
            return;
        }
        ˎ.getWindowVisibleDisplayFrame(this.ॱ);
        if (this.ॱ.left < 0 && this.ॱ.top < 0) {
            Resources resources = this.ˎ.getResources();
            int identifier = resources.getIdentifier("status_bar_height", "dimen", "android");
            if (identifier != 0) {
                identifier = resources.getDimensionPixelSize(identifier);
            } else {
                identifier = 0;
            }
            DisplayMetrics displayMetrics = resources.getDisplayMetrics();
            this.ॱ.set(0, identifier, displayMetrics.widthPixels, displayMetrics.heightPixels);
        }
        ˎ.getLocationOnScreen(this.ʻ);
        view.getLocationOnScreen(this.ʽ);
        int[] iArr = this.ʽ;
        iArr[0] = iArr[0] - this.ʻ[0];
        iArr = this.ʽ;
        iArr[1] = iArr[1] - this.ʻ[1];
        layoutParams.x = (this.ʽ[0] + i) - (ˎ.getWidth() / 2);
        int makeMeasureSpec = MeasureSpec.makeMeasureSpec(0, 0);
        this.ˊ.measure(makeMeasureSpec, makeMeasureSpec);
        makeMeasureSpec = this.ˊ.getMeasuredHeight();
        dimensionPixelOffset = ((dimensionPixelOffset + this.ʽ[1]) - dimensionPixelOffset2) - makeMeasureSpec;
        i3 = (i3 + this.ʽ[1]) + dimensionPixelOffset2;
        if (z) {
            if (dimensionPixelOffset >= 0) {
                layoutParams.y = dimensionPixelOffset;
            } else {
                layoutParams.y = i3;
            }
        } else if (makeMeasureSpec + i3 <= this.ॱ.height()) {
            layoutParams.y = i3;
        } else {
            layoutParams.y = dimensionPixelOffset;
        }
    }

    private static View ˎ(View view) {
        View rootView = view.getRootView();
        ViewGroup.LayoutParams layoutParams = rootView.getLayoutParams();
        if ((layoutParams instanceof LayoutParams) && ((LayoutParams) layoutParams).type == 2) {
            return rootView;
        }
        for (Context context = view.getContext(); context instanceof ContextWrapper; context = ((ContextWrapper) context).getBaseContext()) {
            if (context instanceof Activity) {
                return ((Activity) context).getWindow().getDecorView();
            }
        }
        return rootView;
    }
}
